<?
require("header.php");

/*//切換語言
if (($_GET['module'] == 'changelang') && ( in_array($_GET['v'], $langArray) )) {
$_SESSION['lang'] = $_GET['v'];
header("Location: ".$_SERVER['HTTP_REFERER']);
exit;
}
*/
 

foreach($langArray as $value) {
	switch ($value) {
		case 'zhtw':
				$langMenu .= "<a href='/index.php?module=changelang&v=zhtw' >中文</a> ";
		break;
		case 'enus':
				$langMenu .= "<a href='/index.php?module=changelang&v=enus' >英文</a> ";
		break;
		case 'zhcn':
				$langMenu .= "<a href='/index.php?module=changelang&v=zhcn' >簡體</a> ";
		break;
	}

}

$tpl->assign("js_file",$js_file);
$tpl->assign("css_file",$css_file);
$tpl->assign("register_form",$register_form);
$tpl->assign("lang",$lang);
$tpl->assign("DBlang",$ALLLang[$lang]);
$tpl->assign("langMenu","<span id='langmenu'>".$langMenu."</span>");
$tpl->assign("account_use_idnum",$account_use_idnum);
$tpl->assign("langString",$langString[$lang]);
$tpl->assign("ARTICLEMaxLevel",$ARTICLEMaxLevel);
 
$parent_id = ( $_GET['parent_id'] >0 ) ?  $_GET['parent_id'] : 0;
$parent_id2 = ( $_GET['parent_id2'] >0 ) ?  $_GET['parent_id2'] : 0;
$level = ( $_GET['level'] >0 ) ?  $_GET['level'] : 1;

if ($level > ($ARTICLEMaxLevel+1) ) { //當有人手動改這值時候, 導引到商品首頁
header("Location: article.php");
exit;	
}
  

$group1 = $db->fetch_record_set($db->query("SELECT * from article_group_table where ng_level=1 AND ng_lang = ".GetSQLValueString($ALLLang[$lang] ,"text"). " Order By cast(ng_order AS SIGNED) DESC" ));

 

if ($level <=2) {
$group2 = $db->fetch_record_set($db->query("SELECT * from article_group_table where parent_id=$parent_id AND  ng_lang = ".GetSQLValueString($ALLLang[$lang] ,"text"). " Order By  cast(ng_order AS SIGNED) DESC" )); 
} else {
$group2 = $db->fetch_record_set($db->query("SELECT * from article_group_table where parent_id=$parent_id2 AND  ng_lang = ".GetSQLValueString($ALLLang[$lang] ,"text"). " Order By  cast(ng_order AS SIGNED) DESC" )); 
$group3 = $db->fetch_record_set($db->query("SELECT * from article_group_table where parent_id=$parent_id AND  ng_lang = ".GetSQLValueString($ALLLang[$lang] ,"text"). " Order By  cast(ng_order AS SIGNED) DESC" )); 
$tpl->assign("group3",$group3);
}
// 算商品架路徑 , 由後面往前推算
 

$pathSQL=$db->query("SELECT * from article_group_table where ng_id = ".$parent_id." Order By  cast(ng_order AS SIGNED) DESC" ); 
$l=$level;
while ( $path = $db->fetch_record($pathSQL)) {
 if ($level>=3) {
$pgroup_path = '<a href="/article.php?parent_id2='.$parent_id2 .'&parent_id='.$path['ng_id'].'&level='.$l.'">'.$path['ng_name'].'</a>' . $pgroup_path;	 
 } else {
	  
$pgroup_path = '<a href="/article.php?parent_id='.$path['ng_id'].'&level='.$l.'">'.$path['ng_name'].'</a>' . $pgroup_path;
 }
$l--;
$pathSQL=$db->query("SELECT * from article_group_table where ng_id = ".$path['parent_id']." Order By  cast(ng_order AS SIGNED) DESC" ); 
if ($db->num_rows($pathSQL) > 0) {$pgroup_path  =  " > ". $pgroup_path ; }
}
//

$tpl->assign("level",$level);
$tpl->assign("parent_id",$parent_id);
$tpl->assign("parent_id2",$parent_id2);
 
 


if ($parent_id == 0) { 
$pgroup_path  = $langString[$lang]['AllProduct'];
}
$tpl->assign("pgroup_path",$pgroup_path);



$tpl->assign("group1",$group1);
$tpl->assign("group2",$group2);

if (isset($_GET['n_id'])) {
$article_sql = $db->query("Select * from article_table Where n_enabled = 1 AND n_id = ".GetSQLValueString($_GET['n_id'] ,"text") );

if ($db->num_rows($article_sql) <=0) { header("HTTP/1.0 404 Not Found");  exit; }

$article = $db->fetch_record($article_sql);
$tpl->assign("article",$article);
$tpl->display($lang.'/articledetail.html');
} else {
//商品群組正列
if ($parent_id == 0) { 
$article_sql = $db->query("Select * from article_table Where n_lang = ".GetSQLValueString($ALLLang[$lang] ,"text"). " AND n_enabled = 1  order by cast(n_order AS SIGNED) desc");
} else {
$article_sql = $db->query("Select * from article_table Where n_lang = ".GetSQLValueString($ALLLang[$lang] ,"text"). " AND n_enabled = 1 AND n_group = '".$parent_id."' order by cast(n_order AS SIGNED) desc");
}
$article = $db->fetch_record_set($article_sql);
$arrays = $article; //定義資料集變數
$perpage = 1; //每頁幾個
$maxpage = 3; //每頁顯示最大頁數
if (isset($_GET['pages'])) {
$page = $_GET['pages'];
} else {
$page = 1;
}
if ((count($arrays) % $perpage) ==0)  {
$totalpages =(int)(count($arrays) / $perpage) ; //總頁數
} else {
$totalpages =(int)(count($arrays) / $perpage) + 1; //總頁數
}

//撰寫手動分頁
$key = 0 ; 
while (( ($key + ($page-1) * $perpage) < count($arrays)) && ( $key <  $perpage)) {
//算出每頁的第一筆位子
$shift = ($page-1) * $perpage;
$row_id = $key+$shift;

$article_Array[$key] = $arrays[$row_id];
$article_Array[$key]['detailLink'] = '/article.php?'.$_SERVER['QUERY_STRING']."&n_id=".$arrays[$row_id]['n_id'];
$key += 1;
}

	$prv_page  = $langString[$lang]['pageUp'];
	$next_page = $langString[$lang]['pageDown'];	
			#	------------------------
			#	Searching for $_GET vars
			#	------------------------
			$_get_vars = '';			
			if(!empty($_GET) || !empty($HTTP_GET_VARS)){
				$_GET = empty($_GET) ? $HTTP_GET_VARS : $_GET;
				foreach ($_GET as $_get_name => $_get_value) {
					if ($_get_name != "pages") {
						$_get_vars .= "&$_get_name=".urlencode($_get_value);
					}
				}
			}
if ($page>1) {
$articleList_Path .=" <a  class='pageS_prv' href=\"$_SERVER[PHP_SELF]?pages=".($page-1)."$_get_vars\">$prv_page</a> 　";
} else if ( count($arrays) == 0) {
$articleList_Path .="&nbsp;";
 } else {
$articleList_Path .="<span  class='pageS_prv'>".$prv_page."</span> 　";
}

if ($page <= ($maxpage-1)/2) {
for ($i=1;$i<=$maxpage;$i++) {
if ($i >  $totalpages) break;
	if ($i == $page) {
		$articleList_Path .="<b class='pageS'>$i</b> ";
	} else {
		 $articleList_Path .="<a  class='pageS' href=\"$_SERVER[PHP_SELF]?pages=$i$_get_vars\">$i</a> ";
		//$articleList_Path .="<a href=\"javascript:void(-1);\" onclick='goPage(".($i).")'>$i</a> ";
	}
}
} else if ($page >= ($totalpages -($maxpage-1)/2)) {
 
 for ($i=($totalpages-($maxpage-1));$i<=$totalpages;$i++) {
if ($i >  $totalpages) break;
if  ($i <= 0)    continue;
	if ($i == $page) {
		$articleList_Path .="<b class='pageS'>$i</b> ";
	} else {
		$articleList_Path .="<a  class='pageS' href=\"$_SERVER[PHP_SELF]?pages=$i$_get_vars\">$i</a> ";
//		$articleList_Path .="<a href=\"javascript:void(-1);\" onclick='goPage(".($i).")'>$i</a> ";
	}
}

} else {

 for ($i=($page-($maxpage-1)/2);$i<=($page+($maxpage-1)/2);$i++) {
	 if ($i >  $totalpages) break;
	  
	if ($i == $page) {
		$articleList_Path .="<b class='pageS'>$i</b> ";
	} else {
		$articleList_Path .="<a  class='pageS' href=\"$_SERVER[PHP_SELF]?pages=$i$_get_vars\">$i</a> ";
//$articleList_Path .="<a href=\"javascript:void(-1);\" onclick='goPage(".($i).")'>$i</a> ";
	}
}


}
if ($page<$totalpages) {
$articleList_Path .=" 　<a  class='pageS_next' href=\"$_SERVER[PHP_SELF]?pages=".($page+1)."$_get_vars\">$next_page</a> ";
} else if ( count($arrays) == 0) {
$articleList_Path .="&nbsp;";
 } else {
$articleList_Path .=' 　<span  class="pageS_next">'.$next_page.'</span>';
}



$tpl->assign("pList",$article_Array);
$tpl->assign("pListPath",$articleList_Path);
$tpl->display($lang.'/article.html');	
} // if (isset($_GET['n_id'])) {

?>